/*
 * XXX
 */

package edu.uhcl.csci4838.group3.pendingIntents;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.util.Log;
import edu.uhcl.csci4838.group3.database.BackpackEduDatabaseHelper;
import edu.uhcl.csci4838.group3.database.BackpackEduDatabaseHelper.CursorCourse;
import edu.uhcl.csci4838.group3.database.BackpackEduDatabaseHelper.CursorCourseAlert;
import edu.uhcl.csci4838.group3.database.BackpackEduDatabaseHelper.CursorSemester;
import edu.uhcl.csci4838.group3.database.BackpackEduDatabaseHelper.CursorTask;
import edu.uhcl.csci4838.group3.database.BackpackEduDatabaseHelper.CursorTaskAlert;
import edu.uhcl.csci4838.group3.database.info.CourseAlert;
import edu.uhcl.csci4838.group3.database.info.Semester;
import edu.uhcl.csci4838.group3.database.info.Task;
import edu.uhcl.csci4838.group3.database.info.TaskAlert;
import edu.uhcl.csci4838.group3.date.DateConstants;
import edu.uhcl.csci4838.group3.pendingIntents.notifications.ReminderMngr;
import edu.uhcl.csci4838.group3.pendingIntents.ringer.RingerIntent;

/**
 * XXX methods that all intents use
 * 
 * @author Tyler Hays
 */
public class AllIntents {
	private static final String TAG = "All Intents";
	
	/**
	 * cancel all the intents
	 * 
	 * @param context
	 *            context
	 */
	public static void cancelAllIntents(final Context context) {
		final BackpackEduDatabaseHelper dbhelper = new BackpackEduDatabaseHelper(context);
		
		AllIntents.allTaskCancel(dbhelper,
				context);
		AllIntents.allCourseCancel(dbhelper,
				context);
		
	}
	
	/**
	 * Cancel all course alarms
	 * 
	 * @param dbhelper
	 *            the databehelper
	 * @param context
	 *            context
	 * @param courseCursor
	 *            the course needing to have alarms canceled
	 */
	public static void cancelCourseAlarms(final BackpackEduDatabaseHelper dbhelper,
			final Context context,
			final CursorCourse courseCursor) {
		
		final ReminderMngr reminderMngr = new ReminderMngr(context);
		final long courseID = courseCursor.getColId();
		CursorCourseAlert courseAlertCursor;
		final ContentValues contentValues = new ContentValues();
		contentValues.put(CourseAlert.COURSE_ID,
				Long.valueOf(courseID));
		
		// gets all the alerts for the task
		courseAlertCursor = dbhelper.getCourseAlert(contentValues,
				null,
				true);
		while (courseAlertCursor.isAfterLast() == false) {
			reminderMngr.cancelReminder(courseCursor,
					courseAlertCursor);
			courseAlertCursor.moveToNext();
		}
		
		courseAlertCursor.close();
		
	}
	
	/**
	 * sets up everything that needs to be alerted
	 * 
	 * @param dbhelper
	 *            datebase helper
	 * @param context
	 *            current context
	 * @param taskCursor
	 *            task Cursor being cancel
	 */
	public static void cancelTaskAlarms(final BackpackEduDatabaseHelper dbhelper,
			final Context context,
			final CursorTask taskCursor) {
		long taskID;
		ContentValues contentValues;
		final ReminderMngr reminderMngr = new ReminderMngr(context);
		taskID = taskCursor.getColId();
		
		contentValues = new ContentValues();
		contentValues.put(TaskAlert.TASK_ID,
				Long.valueOf(taskID));
		
		CursorTaskAlert taskAlertCursor;
		
		// gets all the alerts for the task
		taskAlertCursor = dbhelper.getTaskAlert(contentValues,
				null,
				true);
		while (taskAlertCursor.isAfterLast() == false) {
			reminderMngr.cancelReminder(taskCursor,
					taskAlertCursor);
			taskAlertCursor.moveToNext();
		}
		
		taskAlertCursor.close();
		
	}
	
	/**
	 * handles removing all alerts and notification for semeseter when a
	 * semester is delete
	 * 
	 * @param semesterCursor
	 *            semester being deleted
	 * @param dbhelper
	 *            databasehelper
	 * @param context
	 *            context
	 */
	public static void deleteSemester(final CursorSemester semesterCursor,
			final BackpackEduDatabaseHelper dbhelper,
			final Context context) {
		final ContentValues contentValues = new ContentValues();
		CursorCourse courseCursor;
		contentValues.put(Semester.ID,
				semesterCursor.getColId());
		CursorTask taskCursor;
		courseCursor = dbhelper.getCourse(contentValues,
				null,
				true);
		
		// each course in semester
		while (courseCursor.isAfterLast() == false) {
			contentValues.clear();
			AllIntents.cancelCourseAlarms(dbhelper,
					context,
					courseCursor);
			contentValues.put(Task.COURSE_ID,
					Long.valueOf(courseCursor.getColId()));
			taskCursor = dbhelper.getTask(contentValues,
					null,
					true);
			
			// Remove all the alarms for task
			while (taskCursor.isAfterLast() == false) {
				AllIntents.cancelTaskAlarms(dbhelper,
						context,
						taskCursor);
				taskCursor.moveToNext();
			}
			taskCursor.close();
			courseCursor.moveToNext();
		}
		courseCursor.close();
	}
	
	/**
	 * Method to address adding, editing or deleting a holiday
	 * 
	 * @param semesterCursor
	 *            the semeseter having it modified
	 * @param dbhelper
	 *            database helper
	 * @param context
	 *            context
	 */
	public static void modifyHoliday(final CursorSemester semesterCursor,
			final BackpackEduDatabaseHelper dbhelper,
			final Context context) {
		final ContentValues contentValues = new ContentValues();
		CursorCourse courseCursor;
		contentValues.put(Semester.ID,
				Long.valueOf(semesterCursor.getColId()));
		courseCursor = dbhelper.getCourse(contentValues,
				null,
				true);
		RingerIntent ringerIntent;
		// each course in semester
		while (courseCursor.isAfterLast() == false) {
			AllIntents.cancelCourseAlarms(dbhelper,
					context,
					courseCursor);
			ringerIntent = new RingerIntent(context);
			ringerIntent.setUpRingerChange(courseCursor);
			courseCursor.moveToNext();
		}
		courseCursor.close();
	}
	
	/**
	 * generates list of all days from the bit string
	 * 
	 * @param dayBitField
	 *            the day bit field
	 * @return days in course
	 */
	public static List<Integer> setMeetingDays(final int dayBitField) {
		final List<Integer> tempArlInt = new ArrayList<Integer>();
		if ((dayBitField & DateConstants.SUNDAY_BIT) == DateConstants.SUNDAY_BIT) {
			tempArlInt.add(1);
		}
		if ((dayBitField & DateConstants.MONDAY_BIT) == DateConstants.MONDAY_BIT) {
			tempArlInt.add(2);
		}
		if ((dayBitField & DateConstants.TUESDAY_BIT) == DateConstants.TUESDAY_BIT) {
			tempArlInt.add(3);
		}
		if ((dayBitField & DateConstants.WEDNESDAY_BIT) == DateConstants.WEDNESDAY_BIT) {
			tempArlInt.add(4);
		}
		if ((dayBitField & DateConstants.THURSDAY_BIT) == DateConstants.THURSDAY_BIT) {
			tempArlInt.add(5);
		}
		if ((dayBitField & DateConstants.FRIDAY_BIT) == DateConstants.FRIDAY_BIT) {
			tempArlInt.add(6);
		}
		if ((dayBitField & DateConstants.SATURDAY_BIT) == DateConstants.SATURDAY_BIT) {
			tempArlInt.add(7);
		}
		
		return tempArlInt;
	}
	
	/**
	 * All intents being set up
	 * 
	 * @param context
	 *            context needing
	 */
	/* 
	 * (non-Javadoc)
	 * 
	 * @see android.content.BroadcastReceiver#onReceive(android.content.Context, android.content.Intent)
	 */
	
	public static void setUpAllIntents(final Context context) {
		final BackpackEduDatabaseHelper dbhelper = new BackpackEduDatabaseHelper(context);
		
		AllIntents.allTaskSetUp(dbhelper,
				context);
		AllIntents.allCourseSetUp(dbhelper,
				context);
		
		dbhelper.close();
	}
	
	/**
	 * Set up all course alarms
	 * 
	 * @param dbhelper
	 *            database helper
	 * @param context
	 *            the context in use
	 * @param courseCursor
	 *            course needing the alarms
	 */
	public static void setUpCourseAlarms(final BackpackEduDatabaseHelper dbhelper,
			final Context context,
			final CursorCourse courseCursor) {
		long courseID;
		ContentValues contentValues;
		final ReminderMngr reminderMngr = new ReminderMngr(context);
		CursorCourseAlert courseAlertCursor;
		courseID = courseCursor.getColId();
		contentValues = new ContentValues();
		
		contentValues.put(CourseAlert.COURSE_ID,
				Long.valueOf(courseID));
		
		// gets all the alerts for the task
		courseAlertCursor = dbhelper.getCourseAlert(contentValues,
				null,
				true);
		while (courseAlertCursor.isAfterLast() == false) {
			reminderMngr.setReminder(courseCursor,
					courseAlertCursor);
			courseAlertCursor.moveToNext();
		}
		
		courseAlertCursor.close();
		
	}
	
	/**
	 * sets up everything that needs to be alerted
	 * 
	 * @param currentCal
	 *            current time
	 * @param taskDueDate
	 *            due date of the task
	 * @param taskID
	 *            id of Task
	 * @param contentValues
	 *            the contentValue of the task
	 * @param dbhelper
	 *            datebase helper
	 * @param context
	 *            current context
	 * @param taskCursor
	 */
	public static void setUpTaskAlarms(final BackpackEduDatabaseHelper dbhelper,
			final Context context,
			final CursorTask taskCursor) {
		
		final ReminderMngr reminderMngr = new ReminderMngr(context);
		long taskID;
		final ContentValues contentValues = new ContentValues();
		
		taskID = taskCursor.getColId();
		contentValues.put(TaskAlert.TASK_ID,
				Long.valueOf(taskID));
		
		CursorTaskAlert taskAlertCursor;
		
		// gets all the alerts for the task
		taskAlertCursor = dbhelper.getTaskAlert(contentValues,
				null,
				true);
		while (taskAlertCursor.isAfterLast() == false) {
			reminderMngr.setReminder(taskCursor,
					taskAlertCursor);
			taskAlertCursor.moveToNext();
		}
		
		taskAlertCursor.close();
		
	}
	
	/**
	 * Cancel all alerts for all courses
	 * 
	 * @param dbhelper
	 *            database helper
	 * @param context
	 *            context
	 */
	private static void allCourseCancel(final BackpackEduDatabaseHelper dbhelper,
			final Context context) {
		
		CursorCourse courseCursor;
		RingerIntent ringerIntent;
		courseCursor = dbhelper.getCourse(null,
				null,
				true);
		
		/*
		 * This loop moves threw all the task checking if they are  not complete 
		 * or have a due day.  If has due date and not complete is goes set up alerts.
		 */
		while (courseCursor.isAfterLast() == false) {
			ringerIntent = new RingerIntent(context);
			ringerIntent.cancelRingChange(courseCursor);
			AllIntents.cancelCourseAlarms(dbhelper,
					context,
					courseCursor);
			courseCursor.moveToNext();
		}
		courseCursor.close();
		
	}
	
	/**
	 * set up all the course stuff
	 * 
	 * @param dbhelper
	 *            database helper
	 * @param context
	 *            context
	 */
	private static void allCourseSetUp(final BackpackEduDatabaseHelper dbhelper,
			final Context context) {
		
		CursorCourse courseCursor;
		ContentValues contentValues;
		long courseID;
		RingerIntent ringerIntent;
		
		courseCursor = dbhelper.getCourse(null,
				null,
				true);
		
		/*
		 * This loop moves threw all the task checking if they are  not complete 
		 * or have a due day.  If has due date and not complete is goes set up alerts.
		 */
		while (courseCursor.isAfterLast() == false) {
			courseID = courseCursor.getColId();
			contentValues = new ContentValues();
			contentValues.put(CourseAlert.COURSE_ID,
					Long.valueOf(courseID));
			
			ringerIntent = new RingerIntent(context);
			ringerIntent.setUpRingerChange(courseCursor);
			AllIntents.setUpCourseAlarms(dbhelper,
					context,
					courseCursor);
			courseCursor.moveToNext();
		}
		courseCursor.close();
		
	}
	
	/**
	 * all task cancel
	 * 
	 * @param dbhelper
	 *            dbhelper
	 * @param context
	 *            context
	 */
	private static void allTaskCancel(final BackpackEduDatabaseHelper dbhelper,
			final Context context) {
		CursorTask taskCursor;
		Long taskDueDate;
		taskCursor = dbhelper.getTask(null,
				null,
				true);
		
		/*
		 * This loop moves threw all the task checking if they are  not complete 
		 * or have a due day.  If has due date and not complete is goes set up alerts.
		 */
		while (taskCursor.isAfterLast() == false) {
			
			taskDueDate = taskCursor.getColDateDue();
			
			// if there is a due day go set up alert
			if ((taskDueDate != null)
					&& (taskCursor.getColIsCompleted() == false)) {
				AllIntents.cancelTaskAlarms(dbhelper,
						context,
						taskCursor);
			}
			
			taskCursor.moveToNext();
			
		}
		taskCursor.close();
	}
	
	/**
	 * all the taskSet up.
	 * 
	 * @param dbhelper
	 *            database helper
	 * @param context
	 *            context
	 */
	private static void allTaskSetUp(final BackpackEduDatabaseHelper dbhelper,
			final Context context) {
		
		CursorTask taskCursor;
		ContentValues contentValues;
		Long taskDueDate;
		long taskID;
		
		taskCursor = dbhelper.getTask(null,
				null,
				true);
		
		/*
		 * This loop moves threw all the task checking if they are  not complete 
		 * or have a due day.  If has due date and not complete is goes set up alerts.
		 */
		while (taskCursor.isAfterLast() == false) {
			taskID = taskCursor.getColId();
			contentValues = new ContentValues();
			contentValues.put(TaskAlert.TASK_ID,
					Long.valueOf(taskID));
			taskDueDate = taskCursor.getColDateDue();
			
			// if there is a due day go set up alert
			if ((taskDueDate != null)
					&& (taskCursor.getColIsCompleted() == false)) {
				AllIntents.setUpTaskAlarms(dbhelper,
						context,
						taskCursor);
			}
			
			taskCursor.moveToNext();
			
		}
		taskCursor.close();
		
	}
	
}
